package com.situ.mall.controller.back;

import javax.jws.soap.SOAPBinding.Use;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.User;
import com.situ.mall.service.IUserService;

@Controller
@RequestMapping(value="/user")
public class UserController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping(value="/getLoginPage")
	public String name() {
		return "user/login";
	}
	
	//注册
	@RequestMapping(value="/getRegisterPage")
	public String getRegisterPage() {
		return "user/register";
	}
	
	//注册验证数据库重名
	@RequestMapping(value="/checkName")
	@ResponseBody
	public ServerResponse checkName(String username) {
		System.out.println(username);
		return userService.checkName(username);
	}
	
	//注册功能
	@RequestMapping(value="/add")
	@ResponseBody
	public ServerResponse add(User user) {
		System.out.println(user);
		return userService.add(user);
	}
	
	
	//登陆验证
	@RequestMapping(value="/login")
	@ResponseBody
	public ServerResponse login(String username,String password,HttpSession session) {
		ServerResponse response = userService.login(username,password);
		//用户存在
		if (response.isSuccess()) {
			session.setAttribute("UserName", username);
			session.setAttribute("USER", response.getData());//返回给前台数据
		}
		return response;
	}
	
	//验证码验证
	@RequestMapping(value="/YZM")
	@ResponseBody
	public synchronized Boolean input(String checkCode,HttpSession session) {
		/*这里的验证码生成的数字保存在session域中经过login页面直接在这拿出来*/
		String checkCodeSession  = (String)session.getAttribute("checkCodeSession");
		System.out.println(checkCodeSession);
		if (checkCode.equalsIgnoreCase(checkCodeSession)) {
			return true;
		} else {
			return false;
		}
	}
	
	//退出登陆
	@RequestMapping(value="/loginout")
	public String loginout(HttpSession session) {
		session.removeAttribute("username");//销毁session
		session.invalidate();
		return "user/login";
	}
	
	//网页查看用户管理
	@RequestMapping(value="/getUserPage")
	public String getUserPage() {
		return "user/user_list";
	}
	
	//分页数据，默认查找totalcount，layui表格自动添加分页信息：page,limit 
	@RequestMapping(value="/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page,Integer limit,User user){
		ServerResponse serverResponse = userService.pageList(page, limit,user);
		System.out.println(serverResponse);
		return serverResponse;
	}
	
	//Id删除
	@RequestMapping(value="/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		return userService.deleteById(id);
	}
	
	//批量删除
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public ServerResponse deleteAll(String ids) {
		return userService.deleteAll(ids);
	}
	
	//添加页面
	@RequestMapping(value="/getAddPage")
	public String getAddPage() {
		return "user/user_add";
	}
	
	//添加功能
	@RequestMapping(value="/insert")
	@ResponseBody
	public ServerResponse insert(User user) {
		System.out.println(user);
		return userService.insert(user);
	}
	
	//修改页面
	@RequestMapping(value="/getUpdatePage")
	public String getUpdatePage(Integer id, Model model) {
		User user = userService.toUpdate(id);
		System.out.println(id);
		model.addAttribute("user",user);
		return "user/user_update";
	}
	
	//修改功能
	@RequestMapping(value="/update")
	@ResponseBody
	public ServerResponse update(User user) {
		System.out.println(user);
		return userService.update(user);
	}
}
